package com.thinking.project.monitorserver.util;

/**
 * @Description 功能描述：
 * @Author tangsixiang@163.com
 * @DATE 2019/7/2 11:26
 * @Version 1.0
 **/

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtTokenUtil {

    public static String SECRET = "SDFEEdfdeFDRE";
    private static final String PAYLOAD = "payload";


    public static String createToken(String data)   {
        //签发时间
        Date istDate = new Date();

        //设置过期时间
        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.MINUTE, 1);
        Date expiresDate = nowTime.getTime();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("alg", "HS256");
        map.put("typ", "JWT");

        String token = JWT.create()
                .withHeader(map)
                .withClaim("data", data)
                .withExpiresAt(expiresDate)
                .withIssuedAt(istDate)
                .sign(Algorithm.HMAC256(SECRET));

        return token;
    }


    /**
     * @return java.lang.String
     * @Author tangsixiang
     * @Description //TODO
     * @Date 11:37 2019/7/2
     * @Param [object, maxAge]  设置加密字符串 对象   设置过期时间  分钟单位
     ****/
    public static <T> String createToken(T object, int minutes)   {

        //设置过期时间
        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.MINUTE, minutes);
        Date expiresDate = nowTime.getTime();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("alg", "HS256");
        map.put("typ", "JWT");

        String token = null;
        try {
            token = JWT.create()
                    .withHeader(map)
                    .withClaim(PAYLOAD, new ObjectMapper().writeValueAsString(object))   //设置加密数据类型
                    .withExpiresAt(expiresDate)//设置过期时间
                    .withIssuedAt(new Date()) //签发时间
                    .sign(Algorithm.HMAC256(SECRET));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return token;
    }

    public static Map<String, Claim> verifyToken(String token) throws Exception {
        JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SECRET)).build();
        DecodedJWT jwt = null;
        try {
            jwt = verifier.verify(token);
        } catch (Exception e) {
            throw new RuntimeException("凭证过期！");
        }

        return jwt.getClaims();
    }

    public static <T> T verifyToken(String token, Class<T> classT) {
        try {
            JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SECRET)).build();
            DecodedJWT jwt = null;
            jwt = verifier.verify(token);

            String json = jwt.getClaims().get(PAYLOAD).asString();
            return new ObjectMapper().readValue(json, classT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}